package com.mateng.treasurechest.view.recipe.activity

import android.os.Bundle
import android.support.v4.widget.DrawerLayout
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.mateng.treasurechest.R
import com.mateng.treasurechest.base.BaseActivity
import com.mateng.treasurechest.model.recipe.bean.CategoryBean
import com.mateng.treasurechest.model.recipe.bean.MenuSearchBean
import com.mateng.treasurechest.presenter.recipe.RecipePresenter
import com.mateng.treasurechest.utils.PreferencesUtils
import com.mateng.treasurechest.view.recipe.RECIPE_LIST_PAGE_SIZE
import com.mateng.treasurechest.view.recipe.SP
import com.mateng.treasurechest.view.recipe.adapter.MenuSearchAdapter
import com.mateng.treasurechest.view.recipe.adapter.RecipeAllCategoryAdapter
import com.mateng.treasurechest.view.recipe.adapter.RecipeBigCategoryAdapter
import com.mateng.treasurechest.view.recipe.interfaces.IRecipeCategoryView
import kotlinx.android.synthetic.main.activity_recipe_category.*

/**
 * Crated by mateng on 2018/1/25.
 */
class RecipeCategoryActivity : BaseActivity<RecipePresenter>(), IRecipeCategoryView, View.OnClickListener {

    //左侧分类列表Adapter
    private val recipeBigCategoryAdapter = RecipeBigCategoryAdapter(null)
    //右侧侧滑总分类列表Adapter
    private val recipeAllCategoryAdapter = RecipeAllCategoryAdapter(this, null)
    //菜品列表Adapter
    private val menuSearchAdapter = MenuSearchAdapter(null)

    private var categoryId: String? = null

    private var page = 1

    //分类标签数据源
    private var category: CategoryBean.ResultBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recipe_category)
        showBackIcon()
        initView()

        presenter?.iRecipeCategoryView = this

        showProgress()
        presenter?.getRecipeCategory()
    }

    private fun initView() {
        rv_recipe_type.layoutManager = LinearLayoutManager(this)
        rv_recipe_type.adapter = recipeBigCategoryAdapter
        rv_drawer_right.layoutManager = LinearLayoutManager(this)
        rv_drawer_right.adapter = recipeAllCategoryAdapter
        rv_recipe_info.layoutManager = GridLayoutManager(this, 2)
        rv_recipe_info.adapter = menuSearchAdapter

        refreshLayout.setOnLoadmoreListener {
            page += 1
            categoryId?.let { presenter?.getMenuSearch(it, page, RECIPE_LIST_PAGE_SIZE) }
        }

        recipeAllCategoryAdapter.onItemClickListener = object : RecipeAllCategoryAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                PreferencesUtils.putInt(this@RecipeCategoryActivity, SP.SP_KEY_RECIPE_TYPE_POSITION, position, SP.SP_NAME_RECIPE_CACHE)
                //点击查看该大分类下小分类
                category?.childs?.get(position)?.childs?.let { recipeBigCategoryAdapter.setNewData(it) }
                drawer_layout.closeDrawer(Gravity.RIGHT)
            }
        }

        recipeBigCategoryAdapter.onItemClickListener = object : RecipeBigCategoryAdapter.OnItemClickListener {
            override fun onItemClick(cid: String, position: Int) {
                menuSearchAdapter.cleanData()
                presenter?.clearSubscription()
                categoryId = cid
                page = 1
                //点击查看该小分类下标签
                net_progress.visibility = View.VISIBLE
                presenter?.getMenuSearch(cid, page, RECIPE_LIST_PAGE_SIZE)
            }
        }

        drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
    }

    override fun setActionBar(parent: ViewGroup?): View {
        val view = super.setActionBar(parent)
        val menu = view.findViewById<ImageView>(R.id.iv_right)
        menu?.setImageResource(R.mipmap.icon_recipe_menu)
        menu?.setOnClickListener(this)
        return view
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_right ->
                if (drawer_layout.isDrawerOpen(Gravity.RIGHT)) {
                    drawer_layout.closeDrawer(Gravity.RIGHT)
                } else {
                    drawer_layout.openDrawer(Gravity.RIGHT)
                }
        }
    }

    override fun getRecipeCategoryDataSuccess(categoryBean: CategoryBean) {
        hideProgress()
        if (categoryBean.isSuccess()) {
            category = categoryBean.result
            category?.childs?.let { recipeAllCategoryAdapter.setNewData(it) }
        }
    }

    override fun getMenuSearchDataSuccess(menuSearch: MenuSearchBean) {
        net_progress.visibility = View.GONE
        if (menuSearch.isSuccess()) {
            menuSearchAdapter.setNewData(menuSearch.result?.list)
        }
        refreshLayout.finishLoadmore()
    }

    override fun requestError(throwable: Throwable) {
        refreshLayout.finishLoadmore()
        netConnectFailed()
    }


}